package kuang.juc.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallAbleTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread myThread = new MyThread();
        //FutureTask是Runnable的实现类，FutureTask可以传入Callable
        //通过Thread执行FutureTask来执行Callable
        FutureTask<String> futureTask = new FutureTask<>(myThread);
        new Thread(futureTask,"A").start();
        //有缓存，提高效率
        new Thread(futureTask,"B").start();
        //返回值在futureTask中
        String re = futureTask.get();
        System.out.println("返回值是："+re);
    }
}

/**
 * Callable有返回值可以抛出异常，常用
 * Runnable没有返回值，不能抛出异常，不用
 */
class MyThread implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        System.out.println("CALL");
        return "12345";
    }
}
